Fix raylib Text wrapping to measure inline BBCode run sizes (#3532)#3533
Merged
Conversation
raylib's Text wrapped inline BBCode runs at the base font size: a fixed-width Text with an enlarged [FontSize=N]/[FontScale=N] run packed words onto a base-measured line, so the enlarged run spilled past the wrap width. This is the raylib mirror of the XNA-family fix #3529. Two-part fix (mirroring #3529): (1) override IWrappedText.MeasureString(string, int absoluteStartIndexInStrippedText) on the raylib Text so the shared wrap loop measures each styled run at its own resolved size, extracting the shared MeasureStyledLineInBaseUnits helper from GetInlineVariableAwareWidthAndHeight so draw/size/wrap use one per-run resolution; (2) re-wrap after InlineVariables populate in raylib's CustomSetPropertyOnRenderable (make Text.UpdateWrappedText public), since the first wrap runs when RawText is set, before the runs exist. Pins in RaylibGum.Tests: red-first end-to-end wrap tests for a [FontScale=2] run and a [FontSize=N] crisp-swap run (real font measurement, wrap width derived between plain and enlarged single-line widths), plus a fallback pin that MeasureString(string,int) equals the base measure when no inline runs cover the range (plain-text wrapping unchanged). Closes #3532 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…run doesn't overlap the next line (#3532) The wrap-width fix made enlarged inline runs wrap, which exposed a pre-existing raylib draw bug: Render advanced every wrapped line by a uniform base line height (i * LineHeightInPixels), so a line following an enlarged [FontSize]/[FontScale] run was drawn on top of it instead of below. XNA already grows the per-line advance (RenderingLibrary/Graphics/Text.cs); this brings raylib to parity. Extracted GetLineTopOffsets() (public, testable) + GetLineLayoutScale() and had Render consume them only when InlineVariables are present, so the plain-text draw path stays byte-identical and allocation-free. GetLineLayoutScale reuses the same ResolveRunFont resolution DrawStyledLine lays out with (no MeasureTextEx), and the per-line factor matches what GetInlineVariableAwareWidthAndHeight already reports for size. Pin: GetLineTopOffsets_ShouldPushLineBelowEnlargedRun_NotOverlapIt (red-first via a faithful uniform extraction) asserts the line after a [FontScale=2] line sits at twice the base advance. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
raylib's
Textwrapped inline BBCode runs at the base font size. A fixed-width raylibTextwith an enlarged inline run ([FontSize=N]swap or[FontScale=N]multiplier) packed words onto a base-measured line, so the enlarged run then spilled past the wrap width. This is the remaining wrapping half of the raylib work — size reporting was already made font-aware in #3524. It mirrors the XNA-family wrapping fix #3529.Fix
1. Wrap width (the #3532 ask) — mirrors #3529
Runtimes/RaylibGum/Renderables/Text.cs— overrideIWrappedText.MeasureString(string, int absoluteStartIndexInStrippedText)so the shared wrap loop measures each styled run at its own resolved size. Extracted a sharedMeasureStyledLineInBaseUnitshelper out ofGetInlineVariableAwareWidthAndHeight, so the draw path (ResolveRunFont), the size pass, and the new wrap seam all use one per-run resolution and cannot drift. MadeUpdateWrappedText()public.Runtimes/RaylibGum/Renderables/CustomSetPropertyOnRenderable.cs— re-wrap afterInlineVariablespopulate, beforeUpdatePreRenderDimensions(). The first wrap runs whenRawTextis set, before the runs exist, so it is blind to them.2. Per-line vertical advance — raylib parity gap the wrap exposed
Once enlarged runs actually wrapped, a pre-existing raylib draw bug surfaced:
Renderadvanced every wrapped line by a uniform base line height (i * LineHeightInPixels), so a line following an enlarged run was drawn on top of it instead of below. XNA already grows the per-line advance. ExtractedGetLineTopOffsets()(public, testable) +GetLineLayoutScale(), consumed byRenderonly when inline runs are present so the plain-text draw path stays byte-identical and allocation-free. Reuses the sameResolveRunFontresolutionDrawStyledLinelays out with (no extraMeasureTextEx), and matches the per-line height the size pass already reports.Tests (
Tests/RaylibGum.Tests/Renderables/TextMarkupTests.cs)All red-first, using real raylib font measurement (hidden-window atlas):
WrappedText_ShouldRewrapFontAware_WhenBbCodeFontScaleRunWidensLine—[FontScale=2]run; wrap width derived to sit strictly between the plain and enlarged single-line widths. Plain control at the same width stays one line.WrappedText_ShouldRewrapFontAware_WhenBbCodeFontSizeRunWidensLine— the[FontSize=N]crisp-swap-font path via a wired font creator.GetLineTopOffsets_ShouldPushLineBelowEnlargedRun_NotOverlapIt— the line after a[FontScale=2]line sits at twice the base advance (vertical stacking).MeasureString_WithStartIndex_ShouldMatchBaseMeasure_WhenNoInlineVariables— pins that the new overload equals the base measure when no inline runs cover the range, so plain-text wrapping stays byte-identical.Full
RaylibGum.Testssuite: 454 passed, 0 failed.Manual verification
Disposable raylib scratch app: a fixed-width
Textwith an inline[FontScale=3]run wraps within its width AND the wrapped remainder stacks below the enlarged line (no horizontal spill, no vertical overlap), next to a plain control at the same width.Closes #3532
🤖 Generated with Claude Code